<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<!--搜索条件开始-->
<div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>查询条件</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">角色备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark" id="remark" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline">
                        <input type="radio" name="available" value="1" title="可用" >
                        <input type="radio" name="available" value="0" title="不可用" >
                    </div>
                </div>
            </div>


        </div>
        <div class="layui-form-item" style="text-align: center">
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
                <button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
            </div>
        </div>
    </form>
</div>
<!--搜索条件结束-->

<!--数据表格开始-->
<div>
    <table class="layui-hide" id="RoleTable" lay-filter="RoleTable"></table>
    <div id="RoleToolBar" style="display: none;">
        <button type="button" lay-event="addRole" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-add-circle"></span>添加角色</button>
    </div>
    <div id="RoleRowBar" style="display: none">
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
        <button type="button" lay-event="dispatchPermission" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-play"></span>分配角色</button>
    </div>
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="name"  placeholder="请输入角色名称" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否可用:</label>
            <div class="layui-input-block">
                <input type="radio" name="available" value="1" title="可用" checked="">
                <input type="radio" name="available" value="0" title="不可用" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-block"><!-- 这里新建一个编译器 -->
                <textarea class="layui-textarea layui-hide" name="remark" lay-verify="content" id="content"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<!--分配树开始-->
<div style="display: none;padding: 20px" id="dispatchTreeDiv" >
    <ul id="dispatchTree" class="dtree" data-id="0"></ul>
</div>
<!--分配树结束-->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script>
    layui.extend({
        dtree: '/resources/layui_ext/dist/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery','layer','form','laydate','table','layedit','dtree'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var table=layui.table;
        var layedit = layui.layedit;
        var dtree= layui.dtree;

        //初始化富文本编辑器
        var editIndex;

        var tableIndex = table.render({
            elem: '#RoleTable'
            ,url:'/role/loadAllRole'
            ,toolbar: '#RoleToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '登录日志表'
            ,height: 'full-220'
            ,page: true
            ,cols: [ [
                {field:'id', title:'ID',align:'center'}
                ,{field:'name', title:'角色名称',align:'center'}
                ,{field:'remark', title:'角色备注',align:'center'}
                ,{field:'available', title:'是否可用',align:'center',templet:function(d){
                    return d.available==1?'<font color=blue>可用</font>':'<font color=red>不可用</font>';
                }}
                ,{fixed: 'right', title:'操作', toolbar: '#RoleRowBar',align:'center',width:'320'}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: "/role/loadAllRole?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(RoleTable)', function(obj){
            switch(obj.event){
                case 'addRole':
                    openAddRole();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(RoleTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteRole(data);
                    break;
                case 'update':
                    UpdateRole(data);
                    break;
                case 'dispatchPermission':
                    DispatchPermission(data);
                    break;
            };
        });

        var url;
        var mainIndex;

        //添加添加页面
        function openAddRole() {
            mainIndex = layer.open({
                type:1,
                title:'添加公告',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    editIndex=layedit.build('content'); //建立编辑器
                    //清空表单数据
                    layedit.setContent(editIndex,""); //清空富编译器里面的数据 一般的reset清空不了
                    $("#dataFrm")[0].reset();
                    url="/role/addRole";
                }
            });
        }
        //重置数据
        $("#dataFrmResetBtn").click(function(){
            //清空富编译器里面的数据 一般的reset清空不了
            layedit.setContent(editIndex,"");
        });

        //更新页面
        function UpdateRole(data){
            mainIndex = layer.open({
                type:1,
                title:'添加公告',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    editIndex=layedit.build('content'); //建立编辑器
                    //设置富文本里的数据
                    layedit.setContent(editIndex,data.remark);//设置编译器里面的内容
                    form.val("dataFrm",data);
                    url="/role/updateRole";
                }
            });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            layedit.sync(editIndex);  //把富文本里面的数据同步到自己写的textarea里面 这里一定要写到前面
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });



        //删除
        function  DeleteRole(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/role/deleteRole",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        function DispatchPermission(data){
            mainIndex = layer.open({
                type:1,
                title:'分配权限',
                content:$("#dispatchTreeDiv"),
                area:['600px','550px'],
                btn: ['<span class=layui-icon>&#xe605;</span>确认分配', '<span class=layui-icon>&#x1006;</span>关闭窗口'],
                yes: function(index, layero){

                    var params = dtree.getCheckbarNodesParam("dispatchTree");

                    var id = "rid="+data.id; //角色ID

                    $.each(params,function (index,item) {
                        id += "&ids=" + item.nodeId;
                    })
                    $.post("/role/saveRolePermission?"+id, function (res) {
                        layer.msg(res.msg);
                    })
                },
                btn2: function(index, layero){
                        //按钮【按钮二】的回调
                    layer.close(index);
                        //return false 开启该代码可禁止点击该按钮关闭
                },
                btnAlign: 'c',
                success:function(index){
                   // console.log(data);
                    dtree.render({
                        elem: "#dispatchTree",
                        //支持复选树
                        checkbar: true,
                        url: "/role/initPermissionByRoleId?rid="+data.id ,// 使用url加载（可与data加载同时存在）
                        method:'get',//这里要改为get才能用

                        dataStyle:"layuiStyle",
                        response:{message:"msg",statusCode:0}, //默认0成功
                        dataFormat:"list"

                    });
                }
            });
        }

    })
</script>
</body>
</html>